



let http= require('http');
let querystring = require("querystring");
let fs=require('fs');



function info(id){

	return new Promise(function(resolve,reject){
		let bookid=id;

		let header={
			//pc端请求头
				"Accept":"application/json, text/javascript, */*; q=0.01",
				 // "Accept-Encoding":"gzip, deflate, sdch",
				 // "Accept-Language":"zh-CN,zh;q=0.8",
				 // "Cache-Control":"no-cache",
				 // "Connection":"keep-alive",
				 // "Host":"oss.qiaobooks.com",
				 "Origin":"http://web2.qiaobooks.com",
				 // "Pragma":"no-cache",
				 "Referer":"http://web2.qiaobooks.com/read/"+id+".html",
				 "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
			//手机端请求头
			// "Accept":"application/json, text/javascript, */*; q=0.01",
			// "Accept-Encoding":"gzip, deflate, sdch",
			// "Accept-Language":"zh-CN,zh;q=0.8",
			// "Connection":"keep-alive",
			// "Host":"oss.qiaobooks.com",
			// "Origin":"http://www.qiaobooks.com",
			// "Referer":"http://www.qiaobooks.com/read/794",
			// "User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"
		};

		let option={
			hostname:'userbe.oss-cn-shanghai.aliyuncs.com',
			port:'80',
			path:'/book_loc'+bookid,
			method:'get',
			headers:header
		};

		let req=http.request(option,function(res){
			let content='';
			res.setEncoding('utf8');
			res.on('data',function(chunk){

				let str=chunk.toString();
				content+=str;
			});
			res.on('end',function(){
				try {
					content=JSON.parse(content);
					resolve(content);
				}catch(e){
					console.log(e.message);
					fs.readFile('./json/error.log','utf-8',function(err,data){
						if(err) throw Error('读取失败');
						fs.writeFile('./json/error.log',data+bookid+' 信息获取失败\n',function(err){
							if(err)console.log(err);
						})
					})

				}

			})

		});

		req.on('error',function(e){
			console.log(e.message)
		});
		req.write("");

		req.end();
	})
	
}

module.exports.getInfo=info;